<?php

namespace Summer\Hjd\Http\Controllers;

use Illuminate\Routing\Controller;
use RLuders\JWTAuth\Classes\JWTAuth;
use Summer\Hjd\Http\Requests\RgRecordCreateRequest;
use Summer\Hjd\Models\RgRecordModel;
use Summer\Hjd\Models\RgRuleModel;
use Summer\Hub\Http\Controllers\Traits\ApiReturn;
use Input;
use Config;
use Db;
use Exception;
use Summer\Location\Models\UserAddressModel;
use System\Models\File as FileModel;


class RgRecordController extends Controller
{
    use ApiReturn;

    /**
     * 充值申请
     * @param JWTAuth $auth
     * @param RgRecordRequest $request
     */
    public function create(JWTAuth $auth,RgRecordCreateRequest $request){
        if (!$user = $auth->user()) {
            return $this->apiError('登录失效,请重新登录');
        }
        $request->validate();
        $currency_slug=Input::get('currency_slug');
        $money=Input::get('money');
        $pay_type=Input::get('pay_type');
        $remark_user=Input::get('remark_user');
        $pay_type_config=Config::get('summer.hjd::rg_pay_type');
        if(!array_key_exists($pay_type,$pay_type_config)){
            return $this->apiError('请选择正确的支付方式');
        }

        //需要填写地址和银行信息
        if (empty($user->bank_number)){
            return $this->apiError('请完善银行信息');
        }
        $address_count=UserAddressModel::where('user_id',$user->id)->count();
        if(!$address_count){
            return $this->apiError('请完善收货地址');
        }

        //充值规则
        $rule=RgRuleModel::with(['currency'=>function($q){
            $q->select('slug','title','price');
        }])->whereHas('currency',function ($q){
            $q->where('status',1);
        })
        ->where('currency_slug',$currency_slug)
        ->first();
        if(!$rule){
            return $this->apiError('充值规则获取失败');
        }
        if($rule->status!=1){
            return $this->apiError('该币种充值暂时关闭');
        }
        if(($money<$rule->rg_min)&&($rule->rg_min>0)){
            return $this->apiError('最小充值金额:'.$rule->rg_min);
        }
        if(($money>$rule->rg_max)&&($rule->rg_max>0)){
            return $this->apiError('最大充值金额:'.$rule->rg_max);
        }
        if(($money%$rule->rg_mul!=0)&&($rule->rg_mul>0)){
            return $this->apiError($rule->rg_mul.'的整数倍充值');
        }
        //如果是线下支付需要上传支付凭证
        if(in_array($pay_type,['bank','wechat','alipay'])){
            if(!Input::has('pay_cover_id')){
                return $this->apiError('请上传支付凭证');
            }
            $pay_cover_id=Input::get('pay_cover_id',0);
            if($pay_cover_id==0){
                return $this->apiError('请上传支付凭证...');
            }
        }
        //需要支付的金额
        $pay_money=$rule->currency->price*$money;
        Db::beginTransaction();
        try {
            $return=[];
            //创建充值申请记录表
            $rg_record_table=new RgRecordModel();
            $rg_record_table->user_id=$user->id;
            $rg_record_table->currency_slug=$currency_slug;
            $rg_record_table->money=$money;
            $rg_record_table->pay_money=$pay_money;
            $rg_record_table->pay_type=$pay_type;
            $rg_record_table->status=1;
            $rg_record_table->remark_user=$remark_user;
            $rg_record_table->save();
            //如果是线下支付需要上传支付凭证
            if(in_array($pay_type,['bank','wechat','alipay'])){
                $file=FileModel::where('id',$pay_cover_id)->first();
                $rg_record_table->pay_cover()->add($file);
            }
            Db::commit();
            return $this->apiSuccess('操作成功',$return);
        }catch (Exception $e) {
            Db::rollBack();
            return $this->apiError($e->getMessage());
        }
    }

    /**
     * 充值记录
     */
    public function index(JWTAuth $auth)
    {
        if (!$user = $auth->user()) {
            return $this->apiError('登录失效,请重新登录');
        }
        $page=Input::get('page',1);
        $page_size=Input::get('page_size',20);
        $query=RgRecordModel::select();
        $query->with(['currency'=>function($q){
            $q->select('slug','title');
        }]);
        if(Input::has('currency_slug')){
            $currency_slug=Input::get('currency_slug');
            $query->where('currency_slug',$currency_slug);
        }
        if(Input::has('status')){
            $status=Input::get('status');
            $query->where('status',$status);
        }
        $query->where('user_id',$user->id);
        $query->orderBy('id','DESC');
        $records=$query->paginate($page_size,$page);
        $records->each(function ($record){
            $record->status_text=Config::get('summer.hjd::rg_status.'.$record->status);
            $record->pay_type_text=Config::get('summer.hjd::rg_pay_type.'.$record->pay_type);
        });
        $return['records']=$records;
        return $this->apiSuccess('操作成功',$return);
    }
}
